package com.mybatis.moudles.mybatis.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("yidiantest/upload")
public class UploadController {

    @RequestMapping("uploadFile")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            //System.out.println(id);
            File f =new File("D:/files");
            if(!f.exists()){
                f.mkdirs();
            }
            String originalFilename = file.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String ext = originalFilename.substring(lastIndexOf);
            String newFilename = UUID.randomUUID().toString()+ext;
            file.transferTo(new File(f, newFilename));
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }

    }

    @RequestMapping("/test/{id}")
    public String test(@PathVariable("id") String id){
        System.out.println(id);
        return id;
    }
}
